# views.py
import csv

from django.shortcuts import render, redirect, get_object_or_404
from django.http import JsonResponse, HttpResponse
from .models import Employee
from django.views.decorators.csrf import csrf_exempt


# 员工列表视图
@csrf_exempt
def employee_list(request):
    employees = Employee.objects.all()
    return render(request, 'employee-list.html', {'employees': employees})


# 添加员工视图
@csrf_exempt
def employee_add(request):
    if request.method == 'POST':
        Eid = request.POST.get('Eid')
        EName = request.POST.get('EName')
        EPas = request.POST.get('EPas')
        Elevel = request.POST.get('Elevel')
        EtelPhone = request.POST.get('EtelPhone')
        ESalary = request.POST.get('ESalary')
        other = request.POST.get('other')
        Employee.objects.create(
            Eid=Eid,
            EName=EName,
            EPas=EPas,
            Elevel=Elevel,
            EtelPhone=EtelPhone,
            ESalary=ESalary,
            other=other
        )
        return JsonResponse({'code': '200', 'message': '添加成功'})
    return render(request, 'employee-add.html')


# 根据ID查找员工视图
def employee_find_by_id(request):
    Eid = request.GET.get('id')
    employee = get_object_or_404(Employee, Eid=Eid)
    return render(request, 'employee-update.html', {'employee': employee})


# 修改员工视图
@csrf_exempt
def employee_update(request):
    if request.method == 'POST':
        Eid = request.POST.get('Eid')
        employee = get_object_or_404(Employee, Eid=Eid)
        employee.EName = request.POST.get('EName')
        employee.EPas = request.POST.get('EPas')
        employee.Elevel = request.POST.get('Elevel')
        employee.EtelPhone = request.POST.get('EtelPhone')
        employee.ESalary = request.POST.get('ESalary')
        employee.other = request.POST.get('other')
        employee.save()
        return JsonResponse({'code': 200, 'message': '员工信息更新成功'})
    else:
        return JsonResponse({'code': 400, 'message': '请求方法错误'})


# 删除员工视图
@csrf_exempt
def employee_delete(request, employee_id):
    try:
        employee = Employee.objects.get(Eid=employee_id)
        employee.delete()
        return JsonResponse({'code': 200, 'message': '删除成功'})
    except Employee.DoesNotExist:
        return JsonResponse({'code': 404, 'message': '员工不存在'})


def export_employees_csv(request):
    response = HttpResponse(content_type='text/csv; charset=utf-8')
    response['Content-Disposition'] = 'attachment; filename="employees.csv"'

    # 员工数据的字段名，中文表头
    field_names = ['员工编号', '员工姓名', '员工密码', '员工级别', '员工电话', '员工工资', '备注']

    # 写入 CSV 文件
    writer = csv.writer(response, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    writer.writerow([name for name in field_names])  # 写入表头

    # 获取所有员工数据
    employees = Employee.objects.all()

    # 将员工数据写入 CSV 文件
    for employee in employees:
        writer.writerow([
            employee.Eid,
            employee.EName,
            employee.EPas,
            employee.Elevel,
            employee.EtelPhone,
            employee.ESalary,
            employee.other,
        ])

    # 设置 UTF-8 with BOM 编码格式
    response.write('\ufeff'.encode('utf-8'))

    return response
